package com.ruoyi.strategwithtype.service.impl;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;

import com.ruoyi.strategy.mapper.StrategyMapper;
import com.ruoyi.type.mapper.TypeMapper;
import com.ruoyi.vo.StrategyTypeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.strategwithtype.mapper.StrategyTypeMapper;
import com.ruoyi.strategwithtype.domain.StrategyType;
import com.ruoyi.strategwithtype.service.IStrategyTypeService;

/**
 * 策略和类型的中间表Service业务层处理
 *
 * @author ruoyi
 * @date 2025-01-09
 */
@Service
public class StrategyTypeServiceImpl implements IStrategyTypeService
{
    @Autowired
    private StrategyTypeMapper strategyTypeMapper;

    @Autowired
    private StrategyMapper strategyMapper;

    @Autowired
    private TypeMapper typeMapper;

    /**
     * 查询策略和类型的中间表
     *
     * @param strategyId 策略和类型的中间表主键
     * @return 策略和类型的中间表
     */
    @Override
    public StrategyType selectStrategyTypeByStrategyId(Integer strategyId)
    {
        return strategyTypeMapper.selectStrategyTypeByStrategyId(strategyId);
    }

    /**
     * 查询策略和类型的中间表列表
     *
     * @param strategyType 策略和类型的中间表
     * @return 策略和类型的中间表
     */
    @Override
    public List<StrategyTypeVo> selectStrategyTypeList(StrategyType strategyType)
    {
        LinkedList<StrategyTypeVo> stVoList = new LinkedList<>();
        List<StrategyType> lists = strategyTypeMapper.selectStrategyTypeList(strategyType);
        for (StrategyType st : lists) {
            stVoList.add(new StrategyTypeVo( strategyMapper.selectStrategyById(st.getStrategyId()),
                    typeMapper.selectTypeById(st.getTypeId())));
        }
        return stVoList;
    }

    /**
     * 新增策略和类型的中间表
     *
     * @param strategyType 策略和类型的中间表
     * @return 结果
     */
    @Override
    public int insertStrategyType(StrategyType strategyType)
    {
        System.out.println("=========================");
        System.out.println("+++++++++++++11111"+strategyType);
        //判断是否存在
        StrategyType exist = strategyTypeMapper.selectStrategyTypeExist(strategyType.getStrategyId(), strategyType.getTypeId());
        if(!Objects.isNull(exist) && exist.getIsDeleted()==1){ //已经存在
            return -1;
        } else if (!Objects.isNull(exist) && exist.getIsDeleted()==0) {//以前添加过该type，但是取消了，现在is_deleted == 0
            //现在将原来的type，被插入的type覆盖，并且将is_deleted == 1
            exist.setIsDeleted(1);
            return 1;
        }
        return strategyTypeMapper.insertStrategyType(strategyType);
    }

    /**
     * 修改策略和类型的中间表
     *
     * @param strategyType 策略和类型的中间表
     * @return 结果
     */
    @Override
    public int updateStrategyType(StrategyType strategyType)
    {
        //查询这个更新是否已经存在
        return strategyTypeMapper.updateStrategyType(strategyType);
    }

    /**
     * 批量删除策略和类型的中间表
     *
     * @param strategyIds 需要删除的策略和类型的中间表主键
     * @return 结果
     */
    @Override
    public int deleteStrategyTypeByStrategyIds(Integer strategyIds,Integer typeIds)
    {
        return  strategyTypeMapper.deleteStrategyTypeByStrategyIds(strategyIds,typeIds);
    }


}
